* Bootstrap procedure over entire causal link:
capture program drop layersbootstrap
program define layersbootstrap, rclass

	preserve
	bsample if sample == 1, cluster(firmcode) idcluster(newfirmcode)
	
	sort newfirmcode t
	xtset newfirmcode t
	
	* First stage identical for all cases:
	xtreg logsales I_WID I_transport2 yeardummy* sectordummy* if sample == 1, fe
	predict logsalespred, xbu

	* Second stage for organization differs for each model:

	* Young firms
	*************************************
	* GMM, ma(1) errors, one-step
	xtabond2 layers L.layers logsalespred   ///
	sectordummy1-sectordummy8 yeardummy4-yeardummy12 ///
	if sample == 1 & sampleyoung == 1, ivstyle(yeardummy4-yeardummy12) ///
	gmmstyle(L.layers, lag(3 .)) gmmstyle(logsalespred, lag(3 .))
	return scalar layers_sampleyoung = _b[L.layers]
	return scalar sales_sampleyoung = _b[logsalespred]	
	predict layers_gmmma1_sampleyoung
	
	areg log2inequality1 layers_gmmma1_sampleyoung yeardummy* sectordummy* if sample == 1 & sampleyoung == 1, absorb(newfirmcode)
	return scalar log2inequality1l_sampleyoung = _b[layers_gmmma1_sampleyoung]
	areg log2inequality4 layers_gmmma1_sampleyoung yeardummy* sectordummy* if sample == 1 & sampleyoung == 1, absorb(newfirmcode)
	return scalar log2inequality4l_sampleyoung = _b[layers_gmmma1_sampleyoung]
	areg wagegap1_topbottom_log layers_gmmma1_sampleyoung yeardummy* sectordummy* if sample == 1 & sampleyoung == 1, absorb(newfirmcode)
	return scalar log2inequality5l_sampleyoung = _b[layers_gmmma1_sampleyoung]
	
	* Old firms
	*************************************
	* GMM, ma(1) errors, one-step
	xtabond2 layers L.layers logsalespred   ///
	sectordummy1-sectordummy8 yeardummy4-yeardummy12 ///
	if sample == 1 & sampleold == 1, ivstyle(yeardummy4-yeardummy12) ///
	gmmstyle(L.layers, lag(3 .)) gmmstyle(logsalespred, lag(3 .))
	return scalar layers_sampleold = _b[L.layers]
	return scalar sales_sampleold = _b[logsalespred]	
	predict layers_gmmma1_sampleold
	
	areg log2inequality1 layers_gmmma1_sampleold yeardummy* sectordummy* if sample == 1 & sampleold == 1, absorb(newfirmcode)
	return scalar log2inequality1l_sampleold = _b[layers_gmmma1_sampleold]
	areg log2inequality4 layers_gmmma1_sampleold yeardummy* sectordummy* if sample == 1 & sampleold == 1, absorb(newfirmcode)
	return scalar log2inequality4l_sampleold = _b[layers_gmmma1_sampleold]
	areg wagegap1_topbottom_log layers_gmmma1_sampleold yeardummy* sectordummy* if sample == 1 & sampleold == 1, absorb(newfirmcode)
	return scalar log2inequality5l_sampleold = _b[layers_gmmma1_sampleold]
	
	* Multiplant firms
	*************************************
	* GMM, ma(1) errors, one-step
	xtabond2 layers L.layers logsalespred    ///
	sectordummy1-sectordummy8 yeardummy4-yeardummy12 ///
	if sample == 1 & multiplant == 1, ivstyle(yeardummy4-yeardummy12) ///
	gmmstyle(L.layers, lag(3 .) ) gmmstyle(logsalespred, lag(3 .) )
	return scalar layers_multiplant = _b[L.layers]
	return scalar sales_multiplant = _b[logsalespred]	
	predict layers_gmmma1_multiplant

	areg log2inequality1 layers_gmmma1_multiplant yeardummy* sectordummy* if sample == 1 & multiplant == 1, absorb(newfirmcode)
	return scalar log2inequality1l_multiplant = _b[layers_gmmma1_multiplant]
	areg log2inequality4 layers_gmmma1_multiplant yeardummy* sectordummy* if sample == 1 & multiplant == 1, absorb(newfirmcode)
	return scalar log2inequality4l_multiplant = _b[layers_gmmma1_multiplant]
	areg wagegap1_topbottom_log layers_gmmma1_multiplant yeardummy* sectordummy* if sample == 1 & multiplant == 1, absorb(newfirmcode)
	return scalar log2inequality5l_multiplant = _b[layers_gmmma1_multiplant]
	
	* multiproduct firms
	*************************************
	* GMM, ma(1) errors, one-step
	xtabond2 layers L.layers logsalespred    ///
	sectordummy1-sectordummy8 yeardummy4-yeardummy12 ///
	if sample == 1 & multiproduct == 1, ivstyle(yeardummy4-yeardummy12) ///
	gmmstyle(L.layers, lag(3 .) ) gmmstyle(logsalespred, lag(4 .) )	
	return scalar layers_multiproduct = _b[L.layers]
	return scalar sales_multiproduct = _b[logsalespred]	
	predict layers_gmmma1_multiproduct

	areg log2inequality1 layers_gmmma1_multiproduct yeardummy* sectordummy* if sample == 1 & multiproduct == 1, absorb(newfirmcode)
	return scalar log2inequality1l_multiproduct = _b[layers_gmmma1_multiproduct]
	areg log2inequality4 layers_gmmma1_multiproduct yeardummy* sectordummy* if sample == 1 & multiproduct == 1, absorb(newfirmcode)
	return scalar log2inequality4l_multiproduct = _b[layers_gmmma1_multiproduct]
	areg wagegap1_topbottom_log layers_gmmma1_multiproduct yeardummy* sectordummy* if sample == 1 & multiproduct == 1, absorb(newfirmcode)
	return scalar log2inequality5l_multiproduct = _b[layers_gmmma1_multiproduct]

	* Offshoring (HS6) firms
	*************************************
	* GMM, ma(1) errors, one-step
	xtabond2 layers L.layers logsalespred    ///
	sectordummy1-sectordummy8 yeardummy4-yeardummy12 ///
	if sample == 1 & notoffshoring_hs6 == 0, ivstyle(yeardummy4-yeardummy12) ///
	gmmstyle(L.layers, lag(3 .) ) gmmstyle(logsalespred, lag(4 .) )
	return scalar layers_offshoring_hs6 = _b[L.layers]
	return scalar sales_offshoring_hs6 = _b[logsalespred]
	predict layers_gmmma1_offshoring_hs6

	areg log2inequality1 layers_gmmma1_offshoring_hs6 yeardummy* sectordummy* if sample == 1 & notoffshoring_hs6 == 0, absorb(newfirmcode)
	return scalar log2inequality1l_offshoring_hs6 = _b[layers_gmmma1_offshoring_hs6]
	areg log2inequality4 layers_gmmma1_offshoring_hs6 yeardummy* sectordummy* if sample == 1 & notoffshoring_hs6 == 0, absorb(newfirmcode)
	return scalar log2inequality4l_offshoring_hs6 = _b[layers_gmmma1_offshoring_hs6]
	areg wagegap1_topbottom_log layers_gmmma1_offshoring_hs6 yeardummy* sectordummy* if sample == 1 & notoffshoring_hs6 == 0, absorb(newfirmcode)
	return scalar log2inequality5l_offshoring_hs6 = _b[layers_gmmma1_offshoring_hs6]

	
	* Firms with at least 5 workers
	*************************************
	* GMM, ma(1) errors, one-step
	xtabond2 layers L.layers logsalespred    ///
	sectordummy1-sectordummy8 yeardummy4-yeardummy12 ///
	if sample == 1 & sample1 == 1, ivstyle(yeardummy4-yeardummy12) ///
	gmmstyle(L.layers, lag(3 .) ) gmmstyle(logsalespred, lag(4 .) )
	return scalar layers_sample1 = _b[L.layers]
	return scalar sales_sample1 = _b[logsalespred]	
	predict layers_gmmma1_sample1

	areg log2inequality1 layers_gmmma1_sample1 yeardummy* sectordummy* if sample == 1 & sample1 == 1, absorb(newfirmcode)
	return scalar log2inequality1l_sample1 = _b[layers_gmmma1_sample1]
	areg log2inequality4 layers_gmmma1_sample1 yeardummy* sectordummy* if sample == 1 & sample1 == 1, absorb(newfirmcode)
	return scalar log2inequality4l_sample1 = _b[layers_gmmma1_sample1]
	areg wagegap1_topbottom_log layers_gmmma1_sample1 yeardummy* sectordummy* if sample == 1 & sample1 == 1, absorb(newfirmcode)
	return scalar log2inequality5l_sample1 = _b[layers_gmmma1_sample1]

	
	* Firms with at least 20 workers
	*************************************
	* GMM, ma(1) errors, one-step
	xtabond2 layers L.layers logsalespred    ///
	sectordummy1-sectordummy8 yeardummy4-yeardummy12 ///
	if sample == 1 & sample3 == 1,  ivstyle(yeardummy4-yeardummy12) ///
	gmmstyle(L.layers, lag(3 .)) gmmstyle(logsalespred, lag(4 .))
	return scalar layers_sample3 = _b[L.layers]
	return scalar sales_sample3 = _b[logsalespred]	
	predict layers_gmmma1_sample3
	
	areg log2inequality1 layers_gmmma1_sample3 yeardummy* sectordummy* if sample == 1 & sample3 == 1, absorb(newfirmcode)
	return scalar log2inequality1l_sample3 = _b[layers_gmmma1_sample3]
	areg log2inequality4 layers_gmmma1_sample3 yeardummy* sectordummy* if sample == 1 & sample3 == 1, absorb(newfirmcode)
	return scalar log2inequality4l_sample3 = _b[layers_gmmma1_sample3]
	areg wagegap1_topbottom_log layers_gmmma1_sample3 yeardummy* sectordummy* if sample == 1 & sample3 == 1, absorb(newfirmcode)
	return scalar log2inequality5l_sample3 = _b[layers_gmmma1_sample3]
	
	
	
	
	
	restore
end
